﻿using RpcHelper;
using SqlExecHelper;
using SqlExecHelper.SetColumn;

using Wedonek.RpcStore.Service.Model;

namespace Wedonek.RpcStore.Service.DAL
{
        internal class RemoteServerGroupDAL : SqlBasicClass
        {
                public RemoteServerGroupDAL () : base ("RemoteServerGroup")
                {

                }

                public void Clear (long serverId)
                {
                        base.Drop ("ServerId", serverId);
                }


                internal RemoteGroup[] GetServers (long merId)
                {
                        return base.Get<long, RemoteGroup> ("RpcMerId", merId);
                }

                internal bool CheckIsExists (long merId)
                {
                        return base.CheckIsExists (new ISqlWhere[] {
                                new SqlWhere("RpcMerId", System.Data.SqlDbType.BigInt){Value=merId}
                        });
                }
                internal RemoteGroupAddParam Get (long id)
                {
                        return base.GetRow<long, RemoteGroupAddParam> ("Id", id);
                }

                internal bool CheckIsExists (long merId, long serverId)
                {
                        return base.CheckIsExists (new ISqlWhere[] {
                                new SqlWhere("RpcMerId", System.Data.SqlDbType.BigInt){Value=merId},
                                new SqlWhere("ServerId", System.Data.SqlDbType.BigInt){Value=serverId}
                        });
                }

                internal void DropBind (long id)
                {
                        if (!base.Drop ("Id", id))
                        {
                                throw new ErrorException ("rpc.server.group.clear.error");
                        }
                }
                public void SetWeight (long id, int weight)
                {
                        if (!base.Update (new ISqlSetColumn[]
                        {
                                new SqlSetColumn("Weight", System.Data.SqlDbType.Int){Value=weight}
                        }, "Id", id))
                        {
                                throw new ErrorException ("rpc.server.group.weight.set.error");
                        }
                }
                internal void Add (RemoteGroupAddParam add)
                {
                        base.Insert (add);
                }

                internal RemoteGroup[] GetServers (long rpcMerId, long[] serverId)
                {
                        return base.Get<long, RemoteGroup> ("ServerId", serverId, new SqlWhere ("RpcMerId", System.Data.SqlDbType.BigInt) { Value = rpcMerId });
                }
        }
}
